package com.competition.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.competition.entity.ReceiptInfo;
import com.competition.resp.ResponseResult;
import com.competition.service.ReceiptInfoService;
import com.competition.utils.uuid.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/receiptInfo")
public class ReceiptInfoController {

    @Autowired
    private ReceiptInfoService receiptInfoService;

    @PostMapping("/addReceiptInfo")
    public ResponseResult addReceiptInfo(@RequestBody Map<String, Object> params){
        if(params.get("userId")==null || params.get("receiptPhone")==null||params.get("receiptName")==null || params.get("receiptArea")==null||params.get("receiptAddressDetail")==null){
            return new ResponseResult(300,"参数异常",null);
        }
        String userId = (String) params.get("userId");
        String receiptPhone = (String) params.get("receiptPhone");
        String receiptName = (String) params.get("receiptName");
        String receiptArea = (String) params.get("receiptArea");
        String receiptAddressDetail = (String) params.get("receiptAddressDetail");
        String receiptLabel = (String) params.get("receiptLabel");
        String receiptIschecked = (String) params.get("receiptIschecked");
        String receiptCity = (String) params.get("receiptCity");
        String receiptProvince = (String) params.get("receiptProvince");
        ReceiptInfo receiptInfo = new ReceiptInfo(UUIDUtil.getUUID(),userId,receiptName,receiptPhone,receiptArea,receiptAddressDetail,receiptIschecked,receiptLabel,receiptCity,receiptProvince);

//        ReceiptInfo receiptInfo = new ReceiptInfo(UUIDUtil.getUUID(),userId,receiptName,receiptPhone,receiptArea,receiptAddress,null,null,null,null);

        try {
            boolean save = receiptInfoService.save(receiptInfo);
            if (save){
                return new ResponseResult(200,"添加成功",null);
            }else {
                return new ResponseResult(300,"添加失败",null);
            }
        } catch (Exception e) {
            return new ResponseResult(300,"添加失败",null);
        }

    }
    @GetMapping("/getReceiptInfoById")
    public ResponseResult getReceiptInfoById(@RequestParam("openId")String openId){
        if(openId == null){
            return new ResponseResult(400,"空值",null);
        }
        System.out.println("88888888888888"+openId);
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",openId);
            List<ReceiptInfo> list = receiptInfoService.list(queryWrapper);

            return new ResponseResult(200,"获取成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(300,"获取失败",null);
        }
    }
    @GetMapping("/delReceiptInfoById")
    public ResponseResult delReceiptInfoById(@RequestParam("receiptId")String receiptId){
        if (receiptId == null){
            return new ResponseResult(400,"空值",null);
        }
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("receipt_id",receiptId);
            boolean remove = receiptInfoService.remove(queryWrapper);
            if (remove){
                return new ResponseResult(200,"删除成功",null);
            }else {
                return new ResponseResult(400,"删除失败",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400,"删除失败",null);
        }

    }
    //根据用户id修改
    @PutMapping("/updReceiptInfoByUserId")
    public ResponseResult updReceiptInfoByUserId(@RequestBody Map<String, Object> params){
        if(params.get("userId")==null){
            return new ResponseResult(300,"参数异常",null);
        }
        String userId = (String) params.get("userId");
        String receiptPhone = (String) params.get("receiptPhone");
        String receiptName = (String) params.get("receiptName");
        String receiptArea = (String) params.get("receiptArea");
        String receiptAddressDetail = (String) params.get("receiptAddressDetail");
        String receiptLabel = (String) params.get("receiptLabel");
        String receiptIschecked = (String) params.get("receiptIschecked");
        String receiptCity = (String) params.get("receiptCity");
        String receiptProvince = (String) params.get("receiptProvince");
        try {
            UpdateWrapper<ReceiptInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id",userId);
            if(receiptPhone!=null &&  !receiptPhone.isEmpty()){
                updateWrapper.set("receipt_phone",receiptPhone);
            }
            if (receiptName != null && !receiptName.isEmpty()) {
                updateWrapper.set("receipt_name", receiptName);
            }
            if (receiptArea != null && !receiptArea.isEmpty()) {
                updateWrapper.set("receipt_area", receiptArea);
            }
            if (receiptAddressDetail != null && !receiptAddressDetail.isEmpty()) {
                updateWrapper.set("receipt_address_detail", receiptAddressDetail);
            }
            if (receiptLabel != null && !receiptLabel.isEmpty()) {
                updateWrapper.set("receipt_label", receiptLabel);
            }
            if (receiptIschecked != null && !receiptIschecked.isEmpty()) {
                updateWrapper.set("receipt_ischecked", receiptIschecked);
            }
            if (receiptCity != null && !receiptCity.isEmpty()) {
                updateWrapper.set("receipt_city", receiptCity);
            }
            if (receiptProvince != null && !receiptProvince.isEmpty()) {
                updateWrapper.set("receipt_province", receiptProvince);
            }

            boolean update = receiptInfoService.update(updateWrapper);
            if (update){
                return new ResponseResult(200,"修改成功",null);
            }else {
                return new ResponseResult(400,"修改失败",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400,"修改失败",null);
        }

    }
    //根据收货地址id修改
    @PutMapping("/updReceiptInfoById")
    public ResponseResult updReceiptInfoById(@RequestBody Map<String, Object> params){
        if(params.get("receiptId")==null){
            return new ResponseResult(300,"参数异常",null);
        }
//        String userId = (String) params.get("userId");
        String receiptId = (String) params.get("receiptId");
        String receiptPhone = (String) params.get("receiptPhone");
        String receiptName = (String) params.get("receiptName");
        String receiptArea = (String) params.get("receiptArea");
        String receiptAddressDetail = (String) params.get("receiptAddressDetail");
        String receiptLabel = (String) params.get("receiptLabel");
        String receiptIschecked = (String) params.get("receiptIschecked");
        String receiptCity = (String) params.get("receiptCity");
        String receiptProvince = (String) params.get("receiptProvince");
        try {
            UpdateWrapper<ReceiptInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("receipt_id",receiptId);
            if(receiptPhone!=null &&  !receiptPhone.isEmpty()){
                updateWrapper.set("receipt_phone",receiptPhone);
            }
            if (receiptName != null && !receiptName.isEmpty()) {
                updateWrapper.set("receipt_name", receiptName);
            }
            if (receiptArea != null && !receiptArea.isEmpty()) {
                updateWrapper.set("receipt_area", receiptArea);
            }
            if (receiptAddressDetail != null && !receiptAddressDetail.isEmpty()) {
                updateWrapper.set("receipt_address_detail", receiptAddressDetail);
            }
            if (receiptLabel != null && !receiptLabel.isEmpty()) {
                updateWrapper.set("receipt_label", receiptLabel);
            }
            if (receiptIschecked != null && !receiptIschecked.isEmpty()) {
                updateWrapper.set("receipt_ischecked", receiptIschecked);
            }
            if (receiptCity != null && !receiptCity.isEmpty()) {
                updateWrapper.set("receipt_city", receiptCity);
            }
            if (receiptProvince != null && !receiptProvince.isEmpty()) {
                updateWrapper.set("receipt_province", receiptProvince);
            }

            boolean update = receiptInfoService.update(updateWrapper);
            if (update){
                return new ResponseResult(200,"修改成功",null);
            }else {
                return new ResponseResult(400,"修改失败",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400,"修改失败",null);
        }

    }

}
